﻿using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq.Dynamic;
using System.Linq;
using TFSBuddy.Settings;
using TFSBuddy.Events;
using System.Windows;
using System.ServiceModel;

namespace TFSBuddy
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class TFSNotifierService : ITFSNotifierService
    {
        private Window mainWindow = null;
        public TFSNotifierService(Window window)
        {
            mainWindow = window;
        }

        public void Notify(String eventXml, String tfsIdentityXml)
        {
            var tfsidentity = EventHelper.CreateInstance<TFSIdentity>(tfsIdentityXml);
            var xmldocument = new XmlDocument();
            //We need to find what event just got triggered, I guess we could create multiple endpoints but I have decided to use one
            xmldocument.LoadXml(eventXml);
            switch (xmldocument.DocumentElement.Name)
            {
                case "WorkItemChangedEvent":
                    ExecuteEvent<WorkItemChangedEvent>(eventXml, EventTypes.WorkItemChangedEvent);
                    break;
                case "BuildCompletionEvent":
                   ExecuteEvent<BuildCompletionEvent>(eventXml,EventTypes.BuildCompletionEvent);
                    break;
                case "NodesDeletedEvent":
                    ExecuteEvent<NodesDeletedEvent>(eventXml, EventTypes.NodesDeletedEvent);
                    break;
                case "BuildStatusChangeEvent":
                    ExecuteEvent<BuildStatusChangeEvent>(eventXml, EventTypes.BuildStatusChangeEvent);
                    break;
                case "ProjectCreatedEvent":
                    ExecuteEvent<ProjectCreatedEvent>(eventXml, EventTypes.ProjectCreatedEvent);
                    break;
                case "BranchMovedEvent":
                    ExecuteEvent<BranchMovedEvent>(eventXml, EventTypes.BranchMovedEvent);
                    break;
                case "ProjectDeletedEvent":
                    ExecuteEvent<ProjectDeletedEvent>(eventXml, EventTypes.ProjectDeletedEvent);
                    break;
                case "NodeCreatedEvent":
                    ExecuteEvent<NodeCreatedEvent>(eventXml, EventTypes.NodeCreatedEvent);
                    break;
                case "CheckinEvent":
                    ExecuteEvent<CheckinEvent>(eventXml, EventTypes.CheckinEvent);
                    break;
                case "NodePropertiesChangedEvent": //When do this trigger?
                    //ExecuteEvent<NodePropertiesChangedEvent>(eventXml, EventTypes.NodePropertiesChangedEvent);
                    break;
                case "NodeRenamedEvent":
                    ExecuteEvent<NodeRenamedEvent>(eventXml, EventTypes.NodeRenamedEvent);
                    break;
            }

            Console.WriteLine(eventXml);
        }
        private void ExecuteEvent<T>(String eventXml,EventTypes eventType) where T : new()
        {
            var tfsevent = EventHelper.CreateInstance<T>(eventXml);
            var eventlist = new List<T> { tfsevent };
            if (Settings.Settings.Default.EventActionFilters != null)
            {
                foreach (var af in Settings.Settings.Default.EventActionFilters.Where(a => a.EventType == eventType))
                {

                    IQueryable<T> filtered = null;

                    if (!string.IsNullOrEmpty(af.Filter))
                    {
                        filtered = eventlist.AsQueryable().Where(af.Filter);
                    }
                    if (string.IsNullOrEmpty(af.Filter) || (filtered != null && filtered.Count() > 0))
                    {
                        IBuddyHelper.ActivateIBuddyEvent(af.States, af.Repeat, IBuddyHelper.GetBuddyList(), mainWindow);
                    }
                }
            }
        }
    }
}
